import { createRouter, createWebHistory, RouterView } from 'vue-router'
import { h } from 'vue'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/home/dashboard'
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/login/clean.vue'),
      meta: { title: '登录' }
    },

    {
      path: '/home',
      component: () => import('../layout/index.vue'),
      redirect: '/home/dashboard',
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('../views/dashboard/index.vue'),
          meta: { title: '首页' }
        },
        {
          path: 'course',
          name: 'Course',
          component: { render: () => h(RouterView) },
          redirect: '/home/course/list',
          meta: { title: '课程管理' },
          children: [
            {
              path: 'list',
              name: 'CourseList',
              component: () => import('../views/course/list.vue'),
              meta: { title: '课程列表' }
            },
            {
              path: 'category',
              name: 'CourseCategory',
              component: () => import('../views/course/category.vue'),
              meta: { title: '科目管理' }
            }
          ]
        },
        {
          path: 'teacher',
          name: 'Teacher',
          component: { render: () => h(RouterView) },
          redirect: '/home/teacher/list',
          meta: { title: '教师管理' },
          children: [
            {
              path: 'list',
              name: 'TeacherList',
              component: () => import('../views/teacher/list.vue'),
              meta: { title: '教师列表' }
            },
            {
              path: 'review',
              name: 'TeacherReview',
              component: () => import('../views/teacher/review.vue'),
              meta: { title: '教师审核' }
            }
          ]
        },
        {
          path: 'coupon',
          name: 'Coupon',
          component: { render: () => h(RouterView) },
          redirect: '/home/coupon/list',
          meta: { title: '优惠券管理' },
          children: [
            {
              path: 'list',
              name: 'CouponList',
              component: () => import('../views/coupon/index.vue'),
              meta: { title: '优惠券列表' }
            },
            {
              path: 'distribution',
              name: 'CouponDistribution',
              component: () => import('../views/coupon/distribution/index.vue'),
              meta: { title: '优惠券发放' }
            }
          ]
        },
        {
          path: 'system',
          name: 'System',
          component: { render: () => h(RouterView) },
          redirect: '/home/system/logs',
          meta: { title: '系统管理' },
          children: [
            {
              path: 'logs',
              name: 'SystemLogs',
              component: () => import('../views/system/logs/index.vue'),
              meta: { title: '操作日志' }
            }
          ]
        },
        {
          path: 'profile',
          name: 'Profile',
          component: () => import('../views/profile/index.vue'),
          meta: { title: '个人信息' }
        }
      ]
    }
  ]
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()
  const hasToken = userStore.token

  if (hasToken) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      try {
        // 如果没有用户信息，先获取用户信息
        if (!userStore.userInfo) {
          await userStore.getUserInfo()
        }
        // 只有在需要权限验证的路由才获取用户信息
        // if (to.meta.requiresAuth && !userStore.userInfo) {
        //   await userStore.getUserInfo()
        // }

        next()
      } catch (error) {
        // token失效，清除用户信息并跳转到登录页
        await userStore.logoutAction()
        next(`/login?redirect=${to.path}`)
        ElMessage.error('登录已过期，请重新登录')
      }
    }
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
    }
  }
})

export default router 
